<script setup>
import { ref, onMounted } from 'vue'
import { Search, Plus, Edit, Delete } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import myAxios from '../../../plugins/myAxios'

// 查询条件
const queryParams = ref({
  studentNumber: '',
  name: '',
  className: '',
  school: '',
  phone: '',
  gender: ''
})

// 表格数据
const tableData = ref([])
const loading = ref(false)

// 分页参数
const pagination = ref({
  current: 1,
  size: 10,
  total: 0
})

// 表格列配置
const columns = [
  { prop: 'studentNumber', label: '学号' },
  { prop: 'name', label: '姓名' },
  { prop: 'className', label: '班级' },
  { prop: 'school', label: '学校' },
  { prop: 'phone', label: '手机号' },
  { prop: 'createTime', label: '创建时间' }
]

// 对话框控制
const dialogVisible = ref(false)
const dialogTitle = ref('')
const formData = ref({})

// 获取学生列表
const getList = async () => {
  loading.value = true
  try {
    const res = await myAxios.get('/admin/student/list', {
      params: {
        ...queryParams.value,
        pageNum: pagination.value.current,
        pageSize: pagination.value.size
      }
    })
    tableData.value = res.records
    pagination.value.total = res.total
  } catch (error) {
    ElMessage.error(error.message || '获取学生列表失败')
  } finally {
    loading.value = false
  }
}

// 重置查询
const resetQuery = () => {
  queryParams.value = {
    studentNumber: '',
    name: '',
    className: '',
    school: '',
    phone: '',
    gender: ''
  }
  getList()
}

// 处理分页变化
const handlePageChange = (val) => {
  pagination.value.current = val
  getList()
}

// 新增学生
const handleAdd = () => {
  dialogTitle.value = '新增学生'
  formData.value = {
    studentNumber: '',
    name: '',
    className: '',
    school: '',
    phone: '',
    gender: ''
  }
  dialogVisible.value = true
}

// 编辑学生
const handleEdit = (row) => {
  dialogTitle.value = '编辑学生'
  formData.value = { ...row }
  dialogVisible.value = true
}

// 删除学生
const handleDelete = (row) => {
  ElMessageBox.confirm('确认删除该学生信息吗？', '提示', {
    type: 'warning'
  }).then(async () => {
    try {
      await myAxios.post(`/admin/student/delete/${row.id}`)
      ElMessage.success('删除成功')
      getList()
    } catch (error) {
      ElMessage.error(error.message || '删除失败')
    }
  })
}

// 提交表单
const handleSubmit = async () => {
  try {
    if (formData.value.id) {
      // 编辑
      await myAxios.post('/admin/student/update', formData.value)
      ElMessage.success('更新成功')
    } else {
      // 新增
      await myAxios.post('/admin/student/add', formData.value)
      ElMessage.success('添加成功')
    }
    dialogVisible.value = false
    getList()
  } catch (error) {
    ElMessage.error(error.message || '操作失败')
  }
}

// 页面加载时获取列表
onMounted(() => {
  getList()
})
</script>

<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-card class="search-card">
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="学号" prop="studentNumber" style="width: 180px;">
          <el-input v-model="queryParams.studentNumber" placeholder="请输入学号" clearable />
        </el-form-item>
        <el-form-item label="姓名" prop="name" style="width: 180px;">
          <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable />
        </el-form-item>
        <el-form-item label="班级" prop="className" style="width: 180px;">
          <el-input v-model="queryParams.className" placeholder="请输入班级" clearable />
        </el-form-item>
        <el-form-item label="学校" prop="school">
          <el-input v-model="queryParams.school" placeholder="请输入学校" clearable />
        </el-form-item>
        <el-form-item label="性别" prop="gender" style="width: 150px;">
          <el-select v-model="queryParams.gender" placeholder="性别" clearable>
            <el-option label="男" :value="1" />
            <el-option label="女" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="getList">搜索</el-button>
          <el-button :icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
        <el-form-item style="position: absolute;right: 60px">
          <el-button type="primary" :icon="Plus" @click="handleAdd">新增学生</el-button>
        </el-form-item>
        <!-- 操作按钮区域 -->

      </el-form>
    </el-card>



    <!-- 表格区域 -->
    <el-table
      v-loading="loading"
      :data="tableData"
      border
      style="width: 100%"
    >
      <el-table-column
        v-for="col in columns"
        :key="col.prop"
        :prop="col.prop"
        :label="col.label"
      />
      <el-table-column label="性别" width="80">
        <template #default="scope">
          {{ scope.row.gender === 1 ? '女' : '男' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" fixed="right">
        <template #default="scope">
          <div class="operation-buttons">
            <el-button 
              type="primary" 
              :icon="Edit" 
              link
              @click="handleEdit(scope.row)"
            >
              编辑
            </el-button>
            <el-button 
              type="danger" 
              :icon="Delete" 
              link
              @click="handleDelete(scope.row)"
            >
              删除
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <div class="pagination">
      <el-pagination
        v-model:current-page="pagination.current"
        v-model:page-size="pagination.size"
        :total="pagination.total"
        @current-change="handlePageChange"
        layout="total, prev, pager, next"
      />
    </div>

    <!-- 新增/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      width="500px"
    >
      <el-form :model="formData" label-width="80px">
        <el-form-item label="学号">
          <el-input v-model="formData.studentNumber" />
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="班级">
          <el-input v-model="formData.className" />
        </el-form-item>
        <el-form-item label="学校">
          <el-input v-model="formData.school" />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="formData.phone" />
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="formData.gender">
            <el-option label="男" :value="1" />
            <el-option label="女" :value="0" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped lang="less">
.app-container {
  padding: 20px;

  .search-card {
    margin-bottom: 20px;
  }

  .toolbar {
    margin-bottom: 20px;
  }

  .pagination {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }

  .operation-buttons {
    display: flex;
    align-items: center;
    gap: 8px;
    
    .el-button {
      padding: 4px 8px;
      height: auto;
    }
  }
}
</style>
